//
//  ZCPageTitleView.swift
//  ZYDY
//
//  Created by zhangchao on 2017/8/21.
//  Copyright © 2017年 zhangchao. All rights reserved.
//

import UIKit

// MARK: - 代理
protocol ZCPageTieleViewDelegate : class {
   func pageTitleView(titleView: ZCPageTitleView, selectedIndex index : Int)
}

private let kScrollLineH : CGFloat = 2
private let kNormalRGB : (CGFloat, CGFloat, CGFloat) = (85, 85, 85)
private let kSelectRGB : (CGFloat, CGFloat, CGFloat) = (251, 127, 29)
private let kDeltaRGB = (kSelectRGB.0 - kNormalRGB.0, kSelectRGB.1 - kNormalRGB.1, kSelectRGB.2 - kNormalRGB.2)

// MARK: - class
class ZCPageTitleView: UIView {
    
    fileprivate var titles : [String]
    fileprivate var currentIndex : Int = 0
    weak var delegate : ZCPageTieleViewDelegate?
    
    fileprivate lazy var titleLabels : [UILabel] = [UILabel]()
    fileprivate lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        
        return scrollView
    }()
    fileprivate lazy var scrollLine : UIView = {
        let scrollLine = UIView()
        scrollLine.backgroundColor = UIColor(r: kSelectRGB.0, g: kSelectRGB.1, b: kSelectRGB.2, alpha: 1.0)
        return scrollLine
    }()

    init(frame: CGRect, titles: [String]) {
        self.titles = titles
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

// MARK: - 设置UI
extension ZCPageTitleView {
    
    fileprivate func setupUI() {
    
        addSubview(scrollView)
        scrollView.frame = bounds
        
        setupTitleLabels()
        setupBottomLineAndScrollLine()
    }
    
    private func setupTitleLabels() {
        let labelW : CGFloat = frame.width / CGFloat(titles.count)
        let labelH : CGFloat = frame.height - kScrollLineH
        let labelY : CGFloat = 0
        
        for (index, title) in titles.enumerated() {
            let label = UILabel()
            
            label.tag = index
            label.text = title
            label.font = UIFont.systemFont(ofSize: 16)
            label.textColor = UIColor(r: kNormalRGB.0, g: kNormalRGB.1, b: kNormalRGB.2, alpha: 1.0)
            label.textAlignment = .center
            
            let labelX : CGFloat = labelW * CGFloat(index)
            label.frame = CGRect(x: labelX, y: labelY, width: labelW, height: labelH)
            
            scrollView.addSubview(label)
            titleLabels.append(label)
            
            label.isUserInteractionEnabled = true
            let tapGes = UITapGestureRecognizer(target: self, action: #selector(self.titleLabelClick(tapGes:)))
            label.addGestureRecognizer(tapGes)
            
            
        }
    }
    
    private func setupBottomLineAndScrollLine () {
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor.lightGray
        let bottomLineH : CGFloat = 0.5
        bottomLine.frame = CGRect(x: 0, y: frame.size.height - bottomLineH, width: frame.size.width, height: bottomLineH)
        addSubview(bottomLine)
        
        guard let firstLabel = titleLabels.first else {
            return
        }
        firstLabel.textColor = UIColor(r: kSelectRGB.0, g: kSelectRGB.1, b: kSelectRGB.2, alpha: 1.0)
        scrollView.addSubview(scrollLine)
        scrollLine.frame = CGRect(x: firstLabel.frame.minX, y: frame.size.height - kScrollLineH, width: firstLabel.frame.width, height: kScrollLineH)
        
    }
}

// MARK: - 点击事件
extension ZCPageTitleView {
    @objc fileprivate func titleLabelClick(tapGes : UITapGestureRecognizer) {
        print("点击titleLabel");
        guard let currentLabel = tapGes.view as? UILabel else {
            return
        }
        
        let oldLabel = titleLabels[currentIndex]
        currentLabel.textColor = UIColor(r: kSelectRGB.0, g: kSelectRGB.1, b: kSelectRGB.2, alpha: 1.0)
        oldLabel.textColor = UIColor(r: kNormalRGB.0, g: kNormalRGB.1, b: kNormalRGB.2, alpha: 1.0)
        
        let scrollLineX = CGFloat(currentLabel.tag) * scrollLine.frame.width
        UIView.animate(withDuration: 0.25) {
            self.scrollLine.frame.origin.x = scrollLineX
        }

        currentIndex = currentLabel.tag

        delegate?.pageTitleView(titleView: self, selectedIndex: currentIndex)
    }
}

// MARK: - 对外接口
extension ZCPageTitleView {
    func setTitleWithProgress(progress : CGFloat, sourceIndex :Int, targetIndex : Int) {
        let sourceLabel = titleLabels[sourceIndex]
        let targetLabel = titleLabels[targetIndex]
        
        let moveTotalX = targetLabel.frame.origin.x - sourceLabel.frame.origin.x
        let moveX = moveTotalX * progress
        UIView.animate(withDuration: 0.25) {
            self.scrollLine.frame.origin.x = sourceLabel.frame.origin.x + moveX
        }
        sourceLabel.textColor = UIColor(r: kSelectRGB.0 - kDeltaRGB.0 * progress, g: kSelectRGB.1 - kDeltaRGB.1 * progress, b: kSelectRGB.2 - kDeltaRGB.2 * progress, alpha: 1.0)
        targetLabel.textColor = UIColor(r: kNormalRGB.0 + kDeltaRGB.0 * progress, g: kNormalRGB.1 + kDeltaRGB.1 * progress, b: kNormalRGB.2 + kDeltaRGB.2 * progress, alpha: 1.0)
        currentIndex = targetIndex
    }
}
